package specialfeatures;



import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;



import javax.xml.parsers.ParserConfigurationException;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Features implements SpecialFeatures {

	public String getLyrics(String artist, String song)	throws IllegalStateException, IOException,
			ParserConfigurationException, SAXException, InterruptedException {

		String artisturi;
		 artisturi=artist.toLowerCase();
		String songuri;
		songuri=song.toLowerCase();
		songuri = songuri.replaceAll(" - ", "-");
		songuri = songuri.replaceAll(" ", "-");
		artisturi = artisturi.replaceAll(" - ", "-");
		artisturi = artisturi.replaceAll(" ", "-");
		

	

		String url = "http://www.leoslyrics.com/"+ artisturi + "/" + songuri+"-lyrics/";

		String document = HttpXml.httpGet(url);

		

		return document;
		

	}

	public URL getCdCover(String artist, String cdName)  {

		String result = null;
		artist=artist.replaceAll(" ", "%20");
		cdName=cdName.replaceAll(" ", "%20");
		String uri = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=532a9737083c4295673a848705a8f29e&artist="
				+ artist + "&album=" + cdName;

		Document document = null;
		try {
			document = HttpXml.getRequest(uri);
		} catch (IOException e) {
			
			e.printStackTrace();
			return null;
		} catch (ParserConfigurationException e) {
			
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			
			e.printStackTrace();
			return null;
		} catch (InterruptedException e) {
			
			e.printStackTrace();
			return null;
		}
		if (document==null) return null;
		else{
		NodeList nl = document.getElementsByTagName("image");
		
		Node nNode = nl.item(nl.getLength() - 1);
		Element eElement = (Element) nNode;
		result = eElement.getTextContent();
		URL url = null;
		if (result==null||result.contains("[")||result.contains("(")) return null;
		else{
		 try {
			url = new URL(result);
		} catch (MalformedURLException e) {
			
			
			return null;
		}
	
		}
		return url;
		}
	}

	public String getWikiPage(String artist) throws IOException {
		
		String uri = "http://en.wikipedia.org/w/api.php?action=opensearch&search="
			+ artist;
		String pageResult=HttpXml.httpGet(uri);
		int size1=pageResult.indexOf(",[");
		int size2=pageResult.length();
		String pageResults;
		
		String uri2=null;
		if (pageResult.contains("[]"))
			pageResults="No page found";
		else{	
		String result = null;
		String result1 = null;
		
		
		
	
		pageResults=pageResult.substring(size1+2, size2-2);
		String[] pages=pageResults.split(",");
		for (int i=0;i<pages.length;i++){
			pages[i]=pages[i].replaceAll("\"", "");
		}
		for (int i=0;i<pages.length;i++){
			if (pages[i].contains("artist")||pages[i].contains("singer")||pages[i].contains("entertainer"))
				result=pages[i];
			if (pages[i].equalsIgnoreCase(artist))
				result1=pages[i];
		}
		if (result==null){
			result=result1;}
		result=result.replaceAll(" " , "_");
		
		 uri2 ="http://en.wikipedia.org/wiki/"+result;
		//content=HttpXml.httpGet(uri2);
		
				
	}
		
		
		
		return uri2;
	}
	public  String getAmazonPage(String artist) {
		String art=artist.replaceAll(" ", "%20");
		String uri="http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords="+art;
		return uri;
	}
	
	
    /**
     * Converts an AWT image to SWT.
     *
     * @param image  the image (<code>null</code> not permitted).
     *
     * @return Image data.
     */
    
}

